﻿using System;
using System.Collections.Generic;
using WindowsGame.Builder;
using WindowsGame.Data;
using WindowsGame.Enums;
using WindowsGame.GameObjects;
using WindowsGame.Manager;
using WindowsGame.Static;
using WindowsGame.Library.Data;
using WindowsGame.Library.IoC;
using WindowsGame.Library.ProxyObjects;
using WindowsGame.Tests.Data;
using NUnit.Framework;

namespace WindowsGame.Tests.Manager
{
	[TestFixture]
	public class DataManagerTests
	{
		// System under test.
		private IDataManager dataManager;
		private GameObjectBuilder gameObjectBuilder;

		[TestFixtureSetUp]
		public void TestFixtureSetUp()
		{
			IoCContainer.Initialize<IDataManager, DataManager>();

			IoCContainer.Initialize<IFileProxy, TestFileProxy>();
			IoCContainer.Initialize<IFileManager, FileManager>();

			IoCContainer.Initialize<GameObjectBuilder, GameObjectBuilder>();
			IoCContainer.Initialize<IGameObjectManager, GameObjectManager>();
			IoCContainer.Initialize<ProxyObjectBuilder, ProxyObjectBuilder>();
			IoCContainer.Initialize<IProxyObjectManager, ProxyObjectManager>();
		}

		[SetUp]
		public void SetUp()
		{
			dataManager = IoCContainer.Resolve<IDataManager>();
			gameObjectBuilder = IoCContainer.Resolve<GameObjectBuilder>();
		}

		[Test]
		public void AllDataTest()
		{
			String fldr = GetFldr();
			String offsetsFile		= GetPath("Offsets.xml");
			String spaceshipsFile	= "Spaceships.xml";
			String weaponsFile		= "Weapons.xml";
			String spaceshipFile	= GetPath("Spaceship.txt");
			String weaponFile		= GetPath("Weapon.txt");

			String[] files = new[] { offsetsFile, spaceshipsFile, weaponsFile, spaceshipFile, weaponFile };
			AllData data = dataManager.LoadData(fldr, files);

			Assert.AreEqual(6, data.Offsets.Length);
			Assert.AreEqual(18, data.SpaceshipList.Count);
			Assert.AreEqual(10, data.WeaponList.Count);
		}

		[Test]
		public void AllDataValidateTest()
		{
			String fldr = GetFldr();
			String offsetsFile		= GetPath("Offsets.xml");
			String spaceshipsFile	= "Spaceships.xml";
			String weaponsFile		= "Weapons.xml";
			String spaceshipFile	= GetPath("Spaceship.txt");
			String weaponFile		= GetPath("Weapon.txt");

			String[] files = new[] { offsetsFile, spaceshipsFile, weaponsFile, spaceshipFile, weaponFile };
			AllData data = dataManager.LoadData(fldr, files);

			Console.WriteLine("SPACESHIPS");
			IList<String> spaceshipList = data.SpaceshipList;
			foreach (String spaceshipType in spaceshipList)
			{
				Spaceship spaceship = gameObjectBuilder.BuildGameObject<Spaceship>(spaceshipType);
				Assert.IsNotNull(spaceship, "Spaceship does not exist: " + spaceshipType);
				Console.WriteLine("Validate OK: " + spaceshipType);
			}

			Console.WriteLine();
			Console.WriteLine("WEAPONS");
			IList<String> weaponList = data.WeaponList;
			foreach (String weaponType in weaponList)
			{
				Weapon weapon = gameObjectBuilder.BuildGameObject<Weapon>(weaponType);
				Assert.IsNotNull(weapon, "Weapon does not exist: " + weaponType);
				Console.WriteLine("Validate OK: " + weaponType);
			}
		}

		private static String GetFldr()
		{
			return GetPath(Constants.DATA_DIRECTORY, String.Empty);
		}
		private static String GetPath(String name)
		{
			return GetPath(Constants.DATA_DIRECTORY, name);
		}
		private static String GetPath(String fldr, String name)
		{
			String root = @"..\..\..\..\WindowsGame\WindowsGame\bin\x86\Debug\";
			String path = String.Format("{0}/{1}/{2}", Constants.ROOT_DIRECTORY, fldr, name);

			return String.Format("{0}{1}", root, path);
		}

		[TestFixtureTearDown]
		public void TestFixtureTearDown()
		{
			IoCContainer.Release();
		}
	}
}